package Zoo.view;

import Zoo.entity.VisitorEntity;
import Zoo.impl.VisitorServiceImpl;
import Zoo.service.VisitorService;
import Zoo.util.TableDTO;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.Vector;

/**
 * 参观者主界面。
 * 参观者没有增删改查功能，因此增删改查按钮没有监听，
 * 也没有参观者主界面处理类（VisitorViewHandler）
 */
public class VisitorView extends JFrame {
    JPanel northPanel = new JPanel();
    JButton addBtn = new JButton("增加");
    JButton updateBtn = new JButton("修改");
    JButton delBtn = new JButton("删除");
    JTextField searchTxt = new JTextField(15);
    JButton searchBtn = new JButton("查询");


    public VisitorView() {
        super("动物园参观者界面");
        Container contentPane = getContentPane();


        //放置北边的组件
        layoutNorth(contentPane);

        Vector<Vector<Object>> data = getVectors();


        //tablemodel：和jtable关联后，之后只需要更新model就能把数据的变化反应到jtable中
        VisitorTableModel visitorTableModel = VisitorTableModel.assembleModel(data);
        //jtable和table关联
        JTable jTable = new JTable(visitorTableModel);
        //设置表头
        JTableHeader tableHeader = jTable.getTableHeader();
        tableHeader.setFont(new Font(null, Font.BOLD, 16));
        tableHeader.setForeground(Color.RED);

        //设置表格体
        jTable.setFont(new Font(null, Font.PLAIN, 14));
        jTable.setForeground(Color.black);
        jTable.setGridColor(Color.BLACK);
        jTable.setRowHeight(30);
        //设置多行选择
        jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        //设置表格列的渲染方式
        Vector<String> columns = visitorTableModel.getColumns();  //获取表头
        VisitorCellRender render = new VisitorCellRender();
        for (int i = 0; i < columns.size(); i++) {
            TableColumn column = jTable.getColumn(columns.get(i));
            column.setCellRenderer(render);
        }


        JScrollPane jScrollPane = new JScrollPane(jTable);
        contentPane.add(jScrollPane);

        setSize(2000, 800);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //setResizable(false);
        setVisible(true);
    }

    //主界面和查询功能（参观者没有查询功能）
    private Vector<Vector<Object>> getVectors() {
        VisitorService visitorService = new VisitorServiceImpl();
        VisitorEntity visitorEntity = new VisitorEntity();
        TableDTO tableDTO = visitorService.retrieveVisitor(visitorEntity);
        Vector<Vector<Object>> data = tableDTO.getData();
        return data;
    }

    //放置北边组件
    private void layoutNorth(Container contentPane) {

        northPanel.add(addBtn);
        northPanel.add(updateBtn);
        northPanel.add(delBtn);
        northPanel.add(searchTxt);
        northPanel.add(searchBtn);
        contentPane.add(northPanel, BorderLayout.NORTH);
    }


    //查询功能的实现
    public void reloadTable() {
        Vector<Vector<Object>> data = getVectors();
        VisitorTableModel visitorTableModel = new VisitorTableModel();
        visitorTableModel.updateModel(data);

    }


}

class VisitorCellRender extends DefaultTableCellRenderer {
    //在每一行的每一列显示之前都会调用
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if (row % 2 == 0) {
            setBackground(Color.LIGHT_GRAY);
        } else {
            setBackground(Color.WHITE);
        }
        setHorizontalAlignment(DefaultTableCellRenderer.CENTER);   //水平居中
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}

//自定义tablemodel
class VisitorTableModel extends DefaultTableModel {
    static Vector<String> columns = new Vector<>();
    private static VisitorTableModel visitorTableModel = new VisitorTableModel();

    //表头
    static {
        columns.addElement("动物名称");
        columns.addElement("动物种类");
        columns.addElement("动物介绍");
        columns.addElement("动物年龄");
        columns.addElement("园区id");
        columns.addElement("展区类型");
        columns.addElement("园区简介");
        columns.addElement("园区占地面积");
        columns.addElement("园区地址");
        columns.addElement("园区开始时间");
        columns.addElement("园区结束时间");
        columns.addElement("园区管理员id");

    }

    //初始的data数据为null
    public VisitorTableModel() {
        super(null, columns);
    }

    //组装data数据
    public static VisitorTableModel assembleModel(Vector<Vector<Object>> data) {
        //组装data数据
        visitorTableModel.setDataVector(data, columns);
        return visitorTableModel;
    }

    //更新data数据
    public static void updateModel(Vector<Vector<Object>> data) {
        visitorTableModel.setDataVector(data, columns);
    }

    //获取表头
    public static Vector<String> getColumns() {
        return columns;
    }

    //单格不可编辑
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }


}